package com.onlinesys.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.onlinesys.beans.UserInfo;
import com.onlinesys.database.DBManager;
import com.onlinesys.service.LoginService;

public class LoginServiceImpl implements LoginService {

    @Override
    public UserInfo login(String username, String password) throws SQLException {
        String sql = "SELECT u.userid userid, u.username username, u.groupid groupid, u.regdate regdate, u.phone phone, u.name name, u.logDate logDate, g.rights rights FROM userinfo u, usergroups g where u.groupid = g.groupid AND u.username = '" + username + "' and u.password = '" + password + "'";
        Connection conn = DBManager.getDBConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        UserInfo ui = new UserInfo();
        while (rs.next()) {
            ui.setUserId(rs.getString("userid"));
            ui.setUserName(rs.getString("username"));
            ui.setUserGroupId(rs.getString("groupid"));
            ui.setName(rs.getString("name"));
            ui.setPhone(rs.getString("phone"));
            ui.setGroupRight(rs.getInt("rights"));
            break;
        }
        return ui;
    }

}
